﻿package view
{
    import com.core.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.sh.game.global.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import view.loader.*;

    public class FlashUIBase extends Sprite implements IPlugin
    {
        protected var mGUI:Sprite;
        protected var TitleBg:Sprite;
        private var mLoader:UIAssetsLoader;
        protected var mContainer:Sprite;
        protected var mGroupID:int = 0;
        public var groupName:String;
        private var mPoint:Point;
        private var mToPoint:Point;
        protected var mData:Object;
        public var closeTime:Number = 0.3;
        private var mFirstOpen:Boolean = true;
        private var mDraging:Boolean = false;
        protected var mShowTween:Boolean = true;
        protected var mDragTopOnly:Boolean = false;
        protected var mCanDrag:Boolean = true;
        protected var mDargWideRange:Array;
        protected var mWeiduan:Boolean = false;
        protected var mAlphaShow:Boolean = true;
        private var _closeTween:TweenMax;
        private var _openTween:TweenMax;
        private var _moveTween:TweenMax;
        private var _hideTween:TweenLite;
        private var _width:Number = 0;
        private var _height:Number = 0;
        public var moveEff:Boolean = false;
        private var _left:int = 0;
        private var _right:int = 0;
        private var _top:int = 0;
        private var _bottom:int = 0;
        private var _cfgGuide:Object = null;
        private var _cfgGuide1:Object = null;
        private var _uiInit:Boolean = false;
        private var _stage:Stage;
        private var defaultPoint:Point;
        private var objArr:Object;
        private static var _pluginstr:String;

        public function FlashUIBase()
        {
            this.mDargWideRange = [];
            this.defaultPoint = new Point();
            this.objArr = {};
            this._stage = GlobalLayer.instance.root;
            this.mLoader = new UIAssetsLoader();
            this.preLoaderInit();
            return;
        }// end function

        override public function get height() : Number
        {
            return this._height > 0 ? (this._height) : (this.mGUI != null ? (this.mGUI.height) : (0));
        }// end function

        override public function get width() : Number
        {
            return this._width > 0 ? (this._width) : (this.mGUI != null ? (this.mGUI.width) : (0));
        }// end function

        public function set showTween(param1:Boolean) : void
        {
            this.mShowTween = param1;
            return;
        }// end function

        protected function preLoaderInit() : void
        {
            return;
        }// end function

        protected function appendLoader(param1:String) : void
        {
            this.mLoader.appendToList(param1);
            return;
        }// end function

        protected function initGUI() : void
        {
            return;
        }// end function

        public function preInitGui() : void
        {
            this.initGUI();
            this.mGUI.visible = false;
            return;
        }// end function

        private function addUIToStage() : void
        {
            var _loc_1:GlowFilter = null;
            if (this.mGUI)
            {
                if (!this.mToPoint)
                {
                    this.mToPoint = this.getDefaultPoint();
                }
                this.mGUI.x = this.mToPoint.x;
                this.mGUI.y = this.mToPoint.y;
                _loc_1 = new GlowFilter(0, 0.3, 4, 4, 3, 1, false, false);
                this.mGUI.filters = [_loc_1];
                this.mGUI.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHandler);
            }
            else
            {
                throw new Error("please override function initGUI() to init ui");
            }
            return;
        }// end function

        protected function initialize() : void
        {
            var _loc_1:View = null;
            if (!this._uiInit)
            {
                if (this.mGUI is View)
                {
                    _loc_1 = this.mGUI as View;
                    if (_loc_1.pluginName != null)
                    {
                        App.skinManager.initHandler(_loc_1.pluginName);
                    }
                }
                this._uiInit = true;
            }
            return;
        }// end function

        protected function openRefresh() : void
        {
            var _loc_1:Object = null;
            var _loc_2:Object = null;
            if (this._cfgGuide.hasOwnProperty(this.groupName))
            {
                for each (_loc_2 in this._cfgGuide[this.groupName])
                {
                    
                    _loc_1 = UserData.getOnlineData("guide");
                    if (_loc_1 && int(_loc_1[_loc_2.id]) == 0 || _loc_2.repeat == 1)
                    {
                        if (_loc_2.type == 5 && _loc_2.typeparam == GuideView.getInstance().gid && this._cfgGuide1[_loc_2.typeparam] && this._cfgGuide1[int(_loc_2.typeparam)].plugin != this.groupName)
                        {
                            GuideView.getInstance().doGuide(_loc_2.id);
                            break;
                        }
                    }
                }
                for each (_loc_2 in this._cfgGuide[this.groupName])
                {
                    
                    _loc_1 = UserData.getOnlineData("guide");
                    if (_loc_1 && int(_loc_1[_loc_2.id]) == 0 || _loc_2.repeat == 1)
                    {
                        if (_loc_2.type == 6)
                        {
                            GuideView.getInstance().doGuide(_loc_2.id);
                            break;
                        }
                    }
                }
            }
            return;
        }// end function

        public function install(param1:Object, param2:String, param3:Number, param4:Number, param5:Number = 0, param6:Number = 0, param7:Number = 0, param8:Number = 0, param9:int = 0) : void
        {
            this.mContainer = param1 as Sprite;
            this.mContainer.addChild(this);
            this.mGroupID = param9;
            this.groupName = param2;
            this._width = param3;
            this._height = param4;
            this._left = param5;
            this._right = param6;
            this._top = param7;
            this._bottom = param8;
            return;
        }// end function

        public function getDefaultPoint() : Point
        {
            if (this._left > 0)
            {
                this.defaultPoint.x = this._left;
            }
            else if (this._right > 0)
            {
                this.defaultPoint.x = GlobalLayer.instance.root.stageWidth - this.width - this._right;
            }
            else
            {
                this.defaultPoint.x = (GlobalLayer.instance.root.stageWidth - this.width) / 2;
            }
            if (this._top > 0)
            {
                this.defaultPoint.y = this._top;
            }
            else if (this._bottom > 0)
            {
                this.defaultPoint.y = GlobalLayer.instance.root.stageHeight - this.height - this._bottom;
            }
            else
            {
                this.defaultPoint.y = (GlobalLayer.instance.root.stageHeight - this.height) / 2;
            }
            return this.defaultPoint;
        }// end function

        protected function loadComplete(event:Event) : void
        {
            this.mLoader.removeEventListener(Event.COMPLETE, this.loadComplete);
            this.mLoader.dispose();
            this.mLoader = null;
            if (this.mGUI == null)
            {
                this.initGUI();
            }
            this.addUIToStage();
            this.addGuide();
            this.mGUI.visible = false;
            this.show();
            return;
        }// end function

        private function addGuide() : void
        {
            var _loc_1:Object = null;
            var _loc_2:String = null;
            var _loc_3:Array = null;
            if (this._cfgGuide == null)
            {
                this._cfgGuide = ConfigDictionary.data.guideplugin;
                this._cfgGuide1 = ConfigDictionary.data.newguide;
            }
            if (this._cfgGuide.hasOwnProperty(this.groupName))
            {
                for each (_loc_1 in this._cfgGuide[this.groupName])
                {
                    
                    if (this.mGUI.hasOwnProperty(_loc_1.eventparam))
                    {
                        this.mGUI[_loc_1.eventparam].gname = _loc_1.eventparam;
                        ModelProxy.change("guideitem", "guideid" + _loc_1.id, this.mGUI[_loc_1.eventparam]);
                        if (!this.objArr[_loc_1.eventparam])
                        {
                            this.objArr[_loc_1.eventparam] = new Array();
                        }
                        this.objArr[_loc_1.eventparam][_loc_1.id] = _loc_1.id;
                    }
                }
                for (_loc_2 in this.objArr)
                {
                    
                    _loc_3 = this.objArr[_loc_2];
                    if (_loc_3.length > 0)
                    {
                        this.mGUI[_loc_2].addEventListener(MouseEvent.CLICK, this.clickHandler);
                    }
                }
            }
            return;
        }// end function

        private function clickHandler(event:Event) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = this.objArr[event.currentTarget.gname];
            for each (_loc_3 in _loc_2)
            {
                
                if (this._cfgGuide1[_loc_3].event == 6)
                {
                    if (GuideViewPao.getInstance().gid == _loc_3)
                    {
                        GuideViewPao.getInstance().hide();
                    }
                    continue;
                }
                ModelProxy.change("guide", "hide", _loc_3);
            }
            return;
        }// end function

        private function mouseDownHandler(event:MouseEvent) : void
        {
            if (this._closeTween)
            {
                return;
            }
            var _loc_2:Boolean = true;
            if (this.mDargWideRange.length > 0)
            {
                _loc_2 = this._stage.mouseY < this.mGUI.y + this.mDargWideRange[0] || (this.mDragTopOnly ? (this._stage.mouseX < this.mGUI.x + this.mDargWideRange[1] || this._stage.mouseX > this.mGUI.x + this.width - this.mDargWideRange[2] || this._stage.mouseY < this.mGUI.y + this.mDargWideRange[0]) : (this._stage.mouseY > this.mGUI.y + this.height - this.mDargWideRange[3]));
            }
            else if (this.mWeiduan)
            {
                _loc_2 = true;
            }
            else
            {
                _loc_2 = this._stage.mouseY < this.mGUI.y + 50 || (this.mDragTopOnly ? (this._stage.mouseX < this.mGUI.x + 15 || this._stage.mouseX > this.mGUI.x + this.width - 15 || this._stage.mouseY < this.mGUI.y + 50) : (this._stage.mouseY > this.mGUI.y + this.height - 20));
            }
            if (this.mCanDrag && _loc_2)
            {
                this._stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
                this._stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHandler);
                this.mDraging = true;
            }
            this.topDisplay();
            return;
        }// end function

        private function mouseUpHandler(event:MouseEvent) : void
        {
            if (this.mDraging)
            {
                this.mDraging = false;
                this._stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
                this._stage.removeEventListener(MouseEvent.MOUSE_UP, this.mouseUpHandler);
                this.mGUI.stopDrag();
                if (this._closeTween)
                {
                    this.fadeComplete2();
                    return;
                }
                if (this.moveEff)
                {
                    if (this._hideTween)
                    {
                        this._hideTween.kill();
                    }
                    this._hideTween = TweenLite.to(this.mGUI, 0.1, {alpha:1, onComplete:this.fadeComplete2});
                }
            }
            return;
        }// end function

        public function topDisplay(param1:String = "") : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (!GuideView.getInstance().visible)
            {
                _pluginstr = "";
            }
            if (this.mContainer.numChildren > 0)
            {
                if (this.parent)
                {
                    _loc_2 = this.mContainer.getChildIndex(this);
                    _loc_3 = this.mContainer.numChildren - 1;
                    if (this.groupName == param1)
                    {
                        if (this.groupName == "taskdialog")
                        {
                            if (GuideView.getInstance().visible)
                            {
                                _loc_3 = this.mContainer.numChildren - 2;
                            }
                        }
                        else
                        {
                            _pluginstr = param1;
                        }
                        if (_loc_2 != _loc_3)
                        {
                            this.mContainer.setChildIndex(this, _loc_3);
                        }
                        return;
                    }
                    else if (this.groupName == "taskdialog")
                    {
                        if (_pluginstr != "")
                        {
                            _loc_3 = this.mContainer.numChildren - 2;
                        }
                    }
                    else if (_pluginstr != "")
                    {
                        if (this.groupName != _pluginstr)
                        {
                            _loc_3 = this.mContainer.numChildren - 2;
                        }
                    }
                    else if (NewGuideView.getInstance().visible && (NewGuideView.getInstance().gid == GuideType.GUIDE1 || NewGuideView.getInstance().gid == GuideType.GUIDE2))
                    {
                        _loc_3 = this.mContainer.numChildren - 2;
                    }
                    if (_loc_2 != _loc_3)
                    {
                        this.mContainer.setChildIndex(this, _loc_3);
                    }
                }
            }
            return;
        }// end function

        private function mouseMoveHandler(event:MouseEvent) : void
        {
            this._stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            this.mGUI.startDrag(false, new Rectangle(0, 0, this._stage.stageWidth - this.width, this._stage.stageHeight - this.height));
            if (this.TitleBg)
            {
                this.TitleBg.visible = false;
            }
            if (this.moveEff)
            {
                if (this._hideTween)
                {
                    this._hideTween.kill();
                }
                this._hideTween = TweenLite.to(this.mGUI, 0.2, {alpha:0.5, onComplete:this.fadeComplete});
            }
            return;
        }// end function

        private function fadeComplete() : void
        {
            this._hideTween = null;
            return;
        }// end function

        private function fadeComplete2() : void
        {
            this._hideTween = null;
            if (this.TitleBg)
            {
                this.TitleBg.visible = true;
            }
            return;
        }// end function

        public function fill(param1:Object) : void
        {
            return;
        }// end function

        public function refresh(param1:Object) : void
        {
            return;
        }// end function

        public function destroy() : void
        {
            return;
        }// end function

        public function returnToTemp() : void
        {
            var _loc_1:* = this.getDefaultPoint();
            this.moveTo(_loc_1.x, _loc_1.y);
            return;
        }// end function

        public function moveTo(param1:int, param2:int) : void
        {
            if (this._closeTween)
            {
                return;
            }
            if (this.mToPoint)
            {
                this.mToPoint.x = param1;
                this.mToPoint.y = param2;
            }
            else
            {
                this.mToPoint = new Point(param1, param2);
            }
            if (this.mGUI)
            {
                if (this._openTween)
                {
                    this._openTween.kill();
                }
                if (this._closeTween)
                {
                    this._closeTween.kill();
                    this._closeTween = null;
                }
                if (this._moveTween)
                {
                    this._moveTween.kill();
                    this._moveTween = null;
                }
                this._moveTween = TweenMax.to(this.mGUI, 0.2, {alpha:1, x:this.mToPoint.x, y:this.mToPoint.y, onComplete:this.moveComplete, ease:Linear.easeOut});
            }
            return;
        }// end function

        private function moveComplete() : void
        {
            this._moveTween = null;
            if (this._openTween)
            {
                this.openComplete();
            }
            return;
        }// end function

        public function open(param1:Point = null, param2:Object = null, param3:Point = null) : void
        {
            this.mPoint = param1;
            this.mData = param2;
            this.mToPoint = param3;
            if (this.mGUI && this._uiInit)
            {
                if (!this.mToPoint)
                {
                    this.mToPoint = this.getDefaultPoint();
                }
                this.show();
            }
            else
            {
                this.mLoader.addEventListener(Event.COMPLETE, this.loadComplete);
                this.mLoader.beginLoad();
            }
            return;
        }// end function

        private function show() : void
        {
            addChild(this.mGUI);
            this.initialize();
            this.topDisplay();
            var _loc_1:* = this.mGUI.visible;
            this.mGUI.visible = true;
            this.openBefore();
            if (this.mPoint && this.mShowTween)
            {
                if (_loc_1)
                {
                    this.mGUI.alpha = 1;
                    this.openComplete();
                    this.openRefresh();
                }
                else
                {
                    this.mGUI.x = this.mToPoint.x;
                    this.mGUI.y = this.mToPoint.y;
                    if (this._moveTween)
                    {
                        this._moveTween.kill();
                        this._moveTween = null;
                    }
                    if (this._closeTween)
                    {
                        this._closeTween.kill();
                        this._closeTween = null;
                    }
                    if (this._openTween)
                    {
                        this._openTween.kill();
                        this._openTween = null;
                    }
                    this.mGUI.alpha = 0;
                    if (this.mAlphaShow)
                    {
                        this._openTween = TweenMax.to(this.mGUI, 0.4, {alpha:1, onComplete:this.openComplete});
                    }
                    else
                    {
                        this.openComplete();
                    }
                }
            }
            else if (_loc_1)
            {
                this.openRefresh();
            }
            else
            {
                this.mGUI.x = this.mToPoint.x;
                this.mGUI.y = this.mToPoint.y;
                this.openComplete();
            }
            return;
        }// end function

        private function openComplete() : void
        {
            this.mGUI.alpha = 1;
            this._openTween = null;
            this.openRefresh();
            PluginManager.instance.openHandler(this.groupName);
            return;
        }// end function

        public function esc() : void
        {
            this.close();
            return;
        }// end function

        public function close() : void
        {
            if (this.mGUI && this.mGUI.visible)
            {
                if (this.mPoint && this.mShowTween)
                {
                    if (this._openTween)
                    {
                        this._openTween.kill();
                        this._openTween = null;
                    }
                    if (this._moveTween)
                    {
                        this._moveTween.kill();
                        this._moveTween = null;
                    }
                    if (!this._closeTween)
                    {
                        this.mGUI.alpha = 1;
                        this._closeTween = TweenMax.to(this.mGUI, this.closeTime, {alpha:0, onComplete:this.closeComplete});
                    }
                }
                else
                {
                    this.closeComplete();
                }
            }
            return;
        }// end function

        protected function closeComplete() : void
        {
            this._closeTween = null;
            this.mGUI.visible = false;
            if (this.mGUI.parent)
            {
                this.mGUI.parent.removeChild(this.mGUI);
            }
            ModelProxy.change("mainui", "closingShake", this.groupName);
            PluginManager.instance.closeHandler(this.groupName);
            return;
        }// end function

        override public function get visible() : Boolean
        {
            if (this._closeTween)
            {
                return false;
            }
            if (this.mGUI)
            {
                return this.mGUI.visible;
            }
            return false;
        }// end function

        public function get Root() : Object
        {
            return null;
        }// end function

        public function get data() : Object
        {
            return this.mData;
        }// end function

        public function get ToPoint() : Point
        {
            var _loc_1:* = this.mToPoint || this.getDefaultPoint();
            this.mToPoint = this.mToPoint || this.getDefaultPoint();
            return _loc_1;
        }// end function

        public function getGuiPostion() : Point
        {
            return new Point(this.mGUI.x, this.mGUI.y);
        }// end function

        public function getGuiSize() : Point
        {
            return new Point(this.mGUI.width, this.mGUI.height);
        }// end function

        protected function openBefore() : void
        {
            return;
        }// end function

    }
}
